﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Pay.Common
{
    /// <summary>
    /// 红包随机金额=扩展
    /// </summary>
    public class HongBaoGetMoneyExtensions
    {
        /// <summary>
        /// 随机金额
        /// </summary>
        /// <param name="remainCount">剩余个数</param>
        /// <param name="remainMoney">剩余金额</param>
        public static List<double> HongBaoGetMoney(int remainCount,double remainMoney)
        {
            //人均最小金额  
            double min = 0.01;
            if (remainMoney < remainCount * min)
                return null;

            int num = remainCount;
            List<double> array = new List<double>();
            Random r = new Random();
            for (int i = 0; i < num; i++)
            {
                if (remainCount == 1)
                {
                    remainCount--;
                    array.Add(Convert.ToDouble(remainMoney.ToString("0.00")));
                }
                else
                {
                    //(_redPacket.remainMoney - (_redPacket.remainCount - 1) * min)：保存剩余金额可以足够的去分配剩余的红包数量  
                    double max = (remainMoney - (remainCount - 1) * min) / remainCount * 2;
                    double money = r.NextDouble() * max;
                    money = Convert.ToDouble((money <= min ? min : money).ToString("0.00"));
                    remainCount--;
                    remainMoney -= money;
                    array.Add(money);
                }
            }
            //再次随机  
            return array.OrderBy(o => Guid.NewGuid()).ToList();
        }
    }
}
